package com.zhoujing.utils;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageCache;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

public class ImageDownLoad {

	public ImageDownLoad() {

	}

	/**
	 * 
	 * @param path
	 * @param context
	 * @param queue
	 * @param imageView
	 */
	public static void downLoadImage(String path, Context context,
			RequestQueue queue, NetworkImageView imageView) {
		int maxSize = ((ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass() / 8;
		final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(
				maxSize) {
			@Override
			protected int sizeOf(String key, Bitmap value) {
				// TODO Auto-generated method stub
				return value.getRowBytes() * value.getHeight();
			}
		};
		ImageCache imageCache = new ImageCache() {
			@Override
			public void putBitmap(String url, Bitmap bitmap) {
				// TODO Auto-generated method stub
				lruCache.put(url, bitmap);
			}

			@Override
			public Bitmap getBitmap(String url) {
				// TODO Auto-generated method stub
				return lruCache.get(url);
			}
		};
		ImageLoader loader = new ImageLoader(queue, imageCache);
		imageView.setTag(path);
		imageView.setImageUrl(path, loader);
	}
}
